package com.threeti.jiayusms.web.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.threeti.jiayusms.domain.model.CommunicationMessage;
import com.threeti.jiayusms.domain.service.communicationmessage.CommunicationMessageQueryService;
import com.threeti.jiayusms.domain.service.communicationmessage.CommunicationMessageUpdateService;
import com.threeti.jiayusms.domain.util.TemplateIdGenerator;
import com.threeti.jiayusms.framework.util.DynaSearchCriteria;
import com.threeti.jiayusms.framework.util.PagedListHandler;
import com.threeti.jiayusms.framework.web.PagedQueryControllerAdapter;
import com.threeti.jiayusms.web.form.MessageTemplateForm;

@Controller
@SessionAttributes("MessageTemplateForm.MessageTemplateDynaCriteria")
public class MessageTemplateMainController {
  private static final Logger logger = LoggerFactory.getLogger(MessageTemplateMainController.class);

  @Autowired
  private CommunicationMessageQueryService queryService;

  @Autowired
  private CommunicationMessageUpdateService updateService;

  @RequestMapping(value="/msgTemplateMain.html")
  public String enterMsgTemplateMain(HttpServletRequest request,Map modelMap) {
    PagedQueryControllerAdapter pagedHelper = new PagedQueryControllerAdapter();
    int[] index = pagedHelper.getIndex(request);
    PagedListHandler<CommunicationMessage> pagedMsgTemplates = queryService.findAllMsgTemplatesByPage(index[0], index[1]);
    pagedHelper.processPage(pagedMsgTemplates, request);

    modelMap.put("msgTemplates", pagedMsgTemplates.getPageList());

    return "message/msgTemplateMain";
  }

  @RequestMapping(value="/searchMsgTemplateByPage.html")
  public String searchMsgTemplate(HttpServletRequest request, @ModelAttribute MessageTemplateForm form, Map modelMap) {

  	DynaSearchCriteria dynaSearchCriteria = null;

    PagedQueryControllerAdapter pagedHelper = new PagedQueryControllerAdapter();
  	if (pagedHelper.isAnyPageSearch(request)) {
  		dynaSearchCriteria = (DynaSearchCriteria)modelMap.get(MessageTemplateForm.SESSION_DYNA_CRITERIA_KEY);
  	} else {
      modelMap.put(MessageTemplateForm.SESSION_DYNA_CRITERIA_KEY, (dynaSearchCriteria = form.buildDynaSearchCriteriaBy("com.threeti.jiayusms.interfaces.MessageTemplateDynaCriteriaDto")));
  	}
    int[] index = pagedHelper.getIndex(request);
    PagedListHandler<CommunicationMessage> pagedMsgTemplates = queryService.findMsgTemplatesByPage(dynaSearchCriteria, index[0], index[1]);
    pagedHelper.processPage(pagedMsgTemplates, request);

    modelMap.put("msgTemplates", pagedMsgTemplates.getPageList());

    return "message/msgTemplateMain";
  }

  @RequestMapping(value="/newMsgTemplate.html", method=RequestMethod.POST)
  public String newMsgTemplate(@ModelAttribute MessageTemplateForm form) {
  	CommunicationMessage msgTemplate = new CommunicationMessage();
  	msgTemplate.setSmsLevel(form.getSmsLevel());
  	msgTemplate.setSmsType(form.getSmsType());
  	msgTemplate.setSmsDescription(form.getSmsDescription());
  	msgTemplate.setSmsContent(form.getSmsContent());

  	CommunicationMessage rtnMsgTemplate = updateService.storeCommunicationMessage(msgTemplate);
  	String templateId = TemplateIdGenerator.generateTempalteIdBy("DX", rtnMsgTemplate);
  	rtnMsgTemplate.setCustomId(templateId);
  	updateService.storeCommunicationMessage(rtnMsgTemplate);

    return "redirect:msgTemplateMain.html";
  }

  @RequestMapping(value="/enterMsgTemplateEditForm.html", method = RequestMethod.GET)
  public String enterEditMsgTemplate(@RequestParam("updatekey") String msgTemplateId, Map modelMap) {
  	CommunicationMessage template = queryService.findCommunicationMessageBy(Long.valueOf(msgTemplateId));

  	modelMap.put("msgTemplate", template);

    return "message/editMsgTemplateForm";
  }

  @RequestMapping(value="/editMsgTemplate.html", method = RequestMethod.POST)
  public String editMsgTemplate(@ModelAttribute MessageTemplateForm form) {
  	CommunicationMessage template = queryService.findCommunicationMessageBy(Long.valueOf(form.getUpdatekey()));
  	template.setSmsLevel(form.getSmsLevel());
  	template.setSmsType(form.getSmsType());
  	template.setSmsContent(form.getSmsContent());
  	template.setSmsDescription(form.getSmsDescription());

  	updateService.storeCommunicationMessage(template);

  	//modelMap.put("msgTemplate", template);

    return "redirect:/msgTemplateMain.html";
  }

  @RequestMapping(value="/removeMsgTemplate.html", method = RequestMethod.POST)
  public String removeMsgTemplate(@RequestParam("keys") String keys,
                            Map modelMap, HttpServletRequest request) {
    String[] msgTemplateIds = keys.split(",");

    for (int i = 0; i < msgTemplateIds.length; i++) {
      updateService.removeCommunicationMessageBy(Long.valueOf(msgTemplateIds[i]));
    }
    return "redirect:/msgTemplateMain.html";
  }
}
